import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import { Row, Col, Input, Select, Button } from 'antd';
import moment from 'moment';
import HealthySearch from './HealthySearch';
import HealthyTable from './HealthyTable';
import api from '../../api/api';
class Healthy extends Component {
  constructor(props) {
    super(props);
    this.state = {
      tableData: [],
      limit: 10,
      skip: 0,
      searchData: {},
      pageSize:10,
      pageIndex:1
    };
  }
  componentWillMount() {
    this.searchData({});
  }
  searchData = async (data,submitClick) => {
    if(submitClick){
      this.setState({
        searchData:data
      })
    }else{
      data = this.state.searchData
    }

    const pageIndex = submitClick ? 1: this.state.pageIndex
    const pageSize = this.state.pageSize
    const skip = pageSize * (pageIndex - 1);
    const limit = pageSize;
    let tableData  = {}
    tableData.list = await api.getMember(data, skip, limit + 1);

    if (tableData.list) {
        if (tableData.list.length > limit) {
            tableData.count = skip + limit + 1;
            tableData.list = tableData.list.slice(0, limit);
        } else {
            tableData.count = skip + (tableData.list ? tableData.list.length : 0);
        }
    } else {//兼容不存在list属性的数据结构
        tableData.count = skip + (tableData.length ? tableData.length : 0);
        tableData.list = Array.isArray(tableData) && tableData.slice(0, limit);
    }
    tableData.pageIndex = Number(submitClick ? 1: this.state.pageIndex)
    tableData.pageSize = Number(this.state.pageSize)

    this.setState({
      tableData,
    });
  };
  pageChange=(pagination)=>{
    // console.log('pagination:',pagination)
    const pageSize = pagination.pageSize;
    const pageIndex = pagination.current;
    this.setState({
      pageSize:pageSize,
      pageIndex:pageIndex
    })
    setTimeout(()=>{
      this.searchData({})
    },0)
  }

  render() {
    return (
      <div>
        <HealthySearch searchData={this.searchData}></HealthySearch>
        <HealthyTable
          tableData={this.state.tableData}
          searchData={this.searchData}
          pageChange={this.pageChange}
        ></HealthyTable>
      </div>
    );
  }
}

export default Healthy;
